﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using PSTUTemplate.Models;
using PSTUTemplate.Models.Object.ViewModels;
using PSTUTemplate.Models.Object;
using PSTUTemplate.Models.DBObject;
using LumenWorks.Framework.IO.Csv;

namespace PSTUTemplate.Controllers
{
    public class ExamResultController : Controller
    {

        Repository rep = new Repository();
        //[Authorize(Roles = "Administrator")]
        [Authorize] 
        //
        // GET: /Exam/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        //
        // POST: /Exam/
        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            if (file.ContentLength > 0)
            {
                UploadCSVFileToExamResult(file);
                ViewBag.Status = "Data Imported Successfully";
            }

            return View();
        }
        private void UploadCSVFileToExamResult(HttpPostedFileBase file)
        {
            string physicalTempFilePath = string.Empty;

            string contentType = file.ContentType.ToLower();

            var Extension = file.FileName.Substring(file.FileName.LastIndexOf('.') + 1).ToLower();

            if (Extension == "csv")
            {
                physicalTempFilePath = DeleteAndSaveCSVFile(physicalTempFilePath, file, contentType);

                CsvReader csv = new CsvReader(new StreamReader(physicalTempFilePath), true);
                csv.MissingFieldAction = MissingFieldAction.ReplaceByNull;

                List<ExamResult> examResultList = new List<ExamResult>();

                string examRol = string.Empty;
                string studentName = string.Empty;
                string regNo = string.Empty;

                string gpa = string.Empty;
                string cGpa = string.Empty;
                string cCH = string.Empty;

                string remarks = string.Empty;

                while (csv.ReadNextRecord())
                {
                    examRol = csv[0].ToString();
                    studentName = csv[1].ToString();

                    regNo = csv[2].ToString();
                    gpa = csv[3].ToString();

                    cGpa = csv[4].ToString();
                    cCH = csv[5].ToString();

                    remarks = csv[6].ToString();

                    AddExamResultToList(examRol, studentName, regNo, gpa, cGpa, cCH, remarks, examResultList);


                    examRol = string.Empty;
                    studentName = string.Empty;

                    regNo = string.Empty;
                    gpa = string.Empty;

                    cGpa = string.Empty;
                    cCH = string.Empty;

                    remarks = string.Empty;

                }
                csv.Dispose();
                InsertToSQLServerExamResultFromExamResult(examResultList);
            }            
        }

        private string DeleteAndSaveCSVFile(string physicalTempFilePath, HttpPostedFileBase inputFile, string contentType)
        {
            if (contentType == "application/vnd.ms-excel"
                || contentType == "application/ms-excel"
                || contentType == "application/x-zip-compressed"
                || contentType == "application/octet-stream"
                || contentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                || contentType == "text/csv")
            {
                var fileName = Path.GetFileName(inputFile.FileName);
                physicalTempFilePath = Path.Combine(Server.MapPath("~/admin/Excell/"), fileName);
                


                inputFile.SaveAs(physicalTempFilePath);

            }
            return physicalTempFilePath;
        }
        private void AddExamResultToList(string examRol, string studentName, string regNo, string gpa, string cGpa, string cCH, string remarks, List<ExamResult> examResultList)
        {
            ExamResult aExamResult = new ExamResult();

            if (!string.IsNullOrEmpty(examRol))
            {
                aExamResult.ExamRoll = examRol;
            }
            if (!string.IsNullOrEmpty(studentName))
            {
                aExamResult.Name = studentName;
            }
            if (!string.IsNullOrEmpty(regNo))
            {
                aExamResult.RegistrationNo = regNo;
            }
            if (!string.IsNullOrEmpty(gpa))
            {
                aExamResult.GPA = Convert.ToDouble(gpa);
            }
            if (!string.IsNullOrEmpty(cGpa))
            {
                aExamResult.CGPA = Convert.ToDouble(cGpa);
            }
            if (!string.IsNullOrEmpty(cCH))
            {
                aExamResult.CCH = Convert.ToDouble(cCH);
            }
            if (!string.IsNullOrEmpty(remarks))
            {
                aExamResult.Remarks = remarks;
            }

            examResultList.Add(aExamResult);
        }

        private void InsertToSQLServerExamResultFromExamResult(List<ExamResult> examResultList)
        {
            ExamResultBLL examResultBLL = new ExamResultBLL();
            DateTime toDay = DateTime.Now;

            foreach (ExamResult item in examResultList)
            {
                examResultBLL.CreateNewExamResult(item);
            }
        }

        [HttpGet]
        public ActionResult Upload()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {

            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Uploads"), fileName);
                file.SaveAs(path);
            }

            return View();
        }
        //public ActionResult Details()
        //{
        //    ExamResultViewModel resultViewModel = new ExamResultViewModel();
        //    resultViewModel.ResultList = rep.GetResultList()

        //}


    }
}
